// Decompiled by GOOSIN CO,LTD Copyright 2010 Pavel Kouznetsov.
// home page: http://www.Goosin.net
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   register_ok.java

package design.design;

import include.nseer_cookie.*;
import include.nseer_db.nseer_db_backup1;
import java.io.IOException;
import java.net.URL;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.servlet.jsp.JspFactory;
import validata.ValidataNumber;
import validata.ValidataTag;

public class register_ok extends HttpServlet
{

	public register_ok()
	{
	}

	public synchronized void service(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException
	{
		HttpSession dbSession = request.getSession();
		JspFactory _jspxFactory = JspFactory.getDefaultFactory();
		javax.servlet.jsp.PageContext pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
		javax.servlet.ServletContext dbApplication = dbSession.getServletContext();
		HttpSession session = request.getSession();
		nseer_db_backup1 design_db = new nseer_db_backup1(dbApplication);
		nseer_db_backup1 manufacture_db = new nseer_db_backup1(dbApplication);
		ValidataNumber validata = new ValidataNumber();
		ValidataTag vt = new ValidataTag();
		counter count = new counter(dbApplication);
		try
		{
			if (design_db.conn((String)dbSession.getAttribute("unit_db_name")) && manufacture_db.conn((String)dbSession.getAttribute("unit_db_name")))
			{
				String product_IDa = request.getParameter("product_IDa");
				String product_namea = request.getParameter("product_namea");
				String product_amount = request.getParameter("product_amount");
				String register_time = request.getParameter("register_time");
				String designer = request.getParameter("designer");
				String register = request.getParameter("register");
				String bodyc = new String(request.getParameter("module_describe").getBytes("UTF-8"), "UTF-8");
				String module_describe = exchange.toHtml(bodyc);
				int num = Integer.parseInt(product_amount);
				int n = 0;
				for (int i = 1; i < num; i++)
				{
					String tem_amount = (new StringBuilder("amount")).append(i).toString();
					String amount = request.getParameter(tem_amount);
					if (!validata.validata(amount))
						n++;
				}

				if (vt.validata((String)dbSession.getAttribute("unit_db_name"), "design_file", "product_ID", product_IDa, "design_module_tag").equals("0"))
				{
					if (n == 0)
					{
						String time = "";
						Date now = new Date();
						SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
						time = formatter.format(now);
						String sql3 = (new StringBuilder("update design_file set design_module_tag='1' where product_ID='")).append(product_IDa).append("'").toString();
						design_db.executeUpdate(sql3);
						String design_ID = NseerId.getId(getClass().getResource("").toString(), (String)dbSession.getAttribute("unit_db_name"));
						try
						{
							String sqll = (new StringBuilder("select * from design_file where product_ID='")).append(product_IDa).append("'").toString();
							ResultSet rs = design_db.executeQuery(sqll);
							if (rs.next())
							{
								String sql = (new StringBuilder("insert into design_module(design_ID,chain_ID,chain_name,product_ID,product_name,module_describe,register_time,designer,register,check_tag,excel_tag) values ('")).append(design_ID).append("','").append(rs.getString("chain_ID")).append("','").append(rs.getString("chain_name")).append("','").append(product_IDa).append("','").append(product_namea).append("','").append(module_describe).append("','").append(register_time).append("','").append(designer).append("','").append(register).append("','0','2')").toString();
								design_db.executeUpdate(sql);
							}
							double cost_price_sum = 0.0D;
							for (int i = 1; i < num; i++)
							{
								String tem_product_name = (new StringBuilder("product_name")).append(i).toString();
								String tem_product_ID = (new StringBuilder("product_ID")).append(i).toString();
								String tem_type = (new StringBuilder("type")).append(i).toString();
								String tem_product_describe = (new StringBuilder("product_describe")).append(i).toString();
								String tem_amount = (new StringBuilder("amount")).append(i).toString();
								String tem_amount_unit = (new StringBuilder("amount_unit")).append(i).toString();
								String tem_cost_price = (new StringBuilder("cost_price")).append(i).toString();
								String product_name = request.getParameter(tem_product_name);
								String product_ID = request.getParameter(tem_product_ID);
								String type = request.getParameter(tem_type);
								String product_describe = request.getParameter(tem_product_describe);
								String amount = request.getParameter(tem_amount);
								String amount_unit = request.getParameter(tem_amount_unit);
								String cost_price2 = request.getParameter(tem_cost_price);
								StringTokenizer tokenTO3 = new StringTokenizer(cost_price2, ",");
								String cost_price;
								String cost_price1;
								for (cost_price = ""; tokenTO3.hasMoreTokens(); cost_price = (new StringBuilder(String.valueOf(cost_price))).append(cost_price1).toString())
									cost_price1 = tokenTO3.nextToken();

								double subtotal = Double.parseDouble(cost_price) * Double.parseDouble(amount);
								cost_price_sum += subtotal;
								String sql1 = (new StringBuilder("insert into design_module_details(design_ID,details_number,product_ID,product_name,type,product_describe,amount,cost_price,amount_unit,subtotal) values ('")).append(design_ID).append("','").append(i).append("','").append(product_ID).append("','").append(product_name).append("','").append(type).append("','").append(product_describe).append("','").append(amount).append("','").append(cost_price).append("','").append(amount_unit).append("','").append(subtotal).append("')").toString();
								design_db.executeUpdate(sql1);
							}

							List rsList = GetWorkflow.getList(design_db, "design_config_workflow", "03");
							if (rsList.size() == 0)
							{
								String sql2 = (new StringBuilder("update design_module set cost_price_sum='")).append(cost_price_sum).append("',check_tag='1' where design_ID='").append(design_ID).append("'").toString();
								design_db.executeUpdate(sql2);
								String procedure_design_ID = "";
								sql3 = (new StringBuilder("select * from manufacture_design_procedure where product_ID='")).append(product_IDa).append("' and check_tag='1' and excel_tag='2'").toString();
								ResultSet rs3 = manufacture_db.executeQuery(sql3);
								if (rs3.next())
									procedure_design_ID = rs3.getString("design_ID");
								String sql4 = (new StringBuilder("update manufacture_design_procedure set design_module_tag='0' where product_ID='")).append(product_IDa).append("' and check_tag='1' and excel_tag='2'").toString();
								manufacture_db.executeUpdate(sql4);
								String sql5 = (new StringBuilder("update manufacture_design_procedure_details set design_module_tag='0' where design_ID='")).append(procedure_design_ID).append("'").toString();
								manufacture_db.executeUpdate(sql5);
								String sql6 = (new StringBuilder("delete from manufacture_design_procedure_module_details where design_ID='")).append(procedure_design_ID).append("'").toString();
								manufacture_db.executeUpdate(sql6);
							} else
							{
								String sql2 = (new StringBuilder("update design_module set cost_price_sum='")).append(cost_price_sum).append("' where design_ID='").append(design_ID).append("'").toString();
								design_db.executeUpdate(sql2);
								String sql;
								for (Iterator ite = rsList.iterator(); ite.hasNext(); design_db.executeUpdate(sql))
								{
									String elem[] = (String[])ite.next();
									sql = (new StringBuilder("insert into design_workflow(config_id,object_ID,describe1,describe2,type_id) values ('")).append(elem[0]).append("','").append(design_ID).append("','").append(elem[1]).append("','").append(elem[2]).append("','03')").toString();
								}

							}
						}
						catch (Exception ex)
						{
							ex.printStackTrace();
						}
						response.sendRedirect("design/design/register_ok_a.jsp");
					} else
					{
						response.sendRedirect((new StringBuilder("design/design/register_ok_b.jsp?product_IDa=")).append(product_IDa).append("&&product_namea=").append(toUtf8String.utf8String(exchange.toURL(product_namea))).toString());
					}
				} else
				{
					response.sendRedirect("design/design/register_ok_c.jsp");
				}
				design_db.commit();
				manufacture_db.commit();
				manufacture_db.close();
				design_db.close();
			} else
			{
				response.sendRedirect("error_conn.htm");
			}
		}
		catch (Exception ex)
		{
			ex.printStackTrace();
		}
	}
}
